package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

/**
 * @author zhangyan
 * @date 2021/2/3 22:45
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDao accountDao;

    @Override
    public void transfer(String fromName, String toName, double money) {

            //1. 根据用户名查询用户信息
            Account fromAccount = accountDao.findByName(fromName);
            Account toAccount = accountDao.findByName(toName);

            //2. 更改账户余额
            fromAccount.setMoney(fromAccount.getMoney() - money);
            toAccount.setMoney(toAccount.getMoney() + money);

            //3. 更新到数据库
            accountDao.update(fromAccount);
            //如果转账过程出现异常,需要事务回滚
            //System.out.println(1 / 0);
            accountDao.update(toAccount);
    }
}
